package cn.com.huzo.service.inventory.impl;

import cn.com.huzo.model.entities.inventory.StockInLinesEntity;
import cn.com.huzo.model.entities.inventory.StockInLinesPoHeaderEntity;
import cn.com.huzo.repository.inventory.StockInLinesPoHeaderRepository;
import cn.com.huzo.repository.inventory.StockInLinesPoLinesRepository;
import cn.com.huzo.repository.inventory.StockInLinesRepository;
import cn.com.huzo.service.inventory.StockInLinesManager;
import cn.com.huzo.service.inventory.StockInLinesPoHeaderManager;
import cn.com.huzo.service.inventory.StockInLinesPoLinesManager;
import huzofw.service.impl.GenericManagerImpl;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 入库单明细---引入入库单主表数据管理服务对象类实现。
 *
 * @author 吴旭
 */
@Service("StockInLinesPoHeaderManager")
public class StockInLinesPoHeaderManagerImpl extends GenericManagerImpl<StockInLinesPoHeaderEntity, String>
        implements StockInLinesPoHeaderManager {

    @Resource(name = "StockInLinesPoHeaderRepository")
    private StockInLinesPoHeaderRepository _dao_po_header;

    @Resource(name = "StockInLinesRepository")
    private StockInLinesRepository _dao_lines;

    @Resource(name = "StockInLinesPoLinesRepository")
    private StockInLinesPoLinesRepository _dao_po_lines;

    @Resource(name = "StockInLinesManager")
    private StockInLinesManager _manager_lines;

    @Resource(name = "StockInLinesPoLinesManager")
    private StockInLinesPoLinesManager _manager_po_lines;

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Transactional
    @Override
    public Map<String, Object> insert(Long headerId, List<Long> poHeaderIdList,
            List<String> poLinesIdList, Integer limit02, Integer limit03, Long userId) throws Exception {
        Map<String, Object> _result = new HashMap();

        // 第一步，增加明细（引入的采购订单表头文件）
        _dao_po_header.insertLines(headerId, poHeaderIdList, userId);

        Map<String, Object> criterion = new HashMap();
        criterion.put("Limit", 15);       // 默认每页显示15行
        criterion.put("PageIndex", 1);    // 当前为第一页

        // 第二步，增加明细（即插入刚刚选择的订单明细）
        Integer rowsAffrected = _dao_po_lines.insert(headerId, poLinesIdList, userId);

        // 第三步，插入入库单明细（这是一个和现有入库明细之间的一个差值）
        criterion.put("HeaderId", headerId);
        criterion.put("CreatedBy", userId);
        criterion.put("UpdatedBy", userId);
        criterion.put("PoLinesIdList", poLinesIdList);
        _dao_lines.insertLines(criterion);

        // 第四步，更新每款的相应材料入库数、到货数
        List<StockInLinesEntity> itemList = _dao_lines.fetchAllByHeaderId(headerId);
        for (int i = 0; i < itemList.size(); i++) {
            _dao_po_lines.updateStockInQuantity(itemList.get(i));
        }
        for (int i = 0; i < itemList.size(); i++) {
            _dao_po_lines.updateStockInQuantity02(itemList.get(i));
        }

        // 得到引入的采购订单信息
        _result.put("LinesPoHeaderList", _dao_po_header.fetchByHeaderKey(headerId));
        // 得到引入的采购订单明细信息
        criterion.put("HeaderId", headerId);
        criterion.replace("Limit", limit02);
        _result.put("LinesPoLinesList", _manager_po_lines.fetchForPage(criterion));
        // 得到新的入库单明细
        criterion.replace("Limit", limit03);
        _result.put("LinesList", _manager_lines.fetchForPage(criterion));

        return _result;
    }
}
